/*
 * sequence.c -- this file is part of mISPL.
 * Copyright (C) 2011 Christian Krause
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
#include "sequence.h"

sequence_t sequence_init(int length, int value) {
	sequence_t sequence = malloc(sizeof(__sequence_s));
	sequence->length = length;
	sequence->period = -1;
	sequence->values = malloc(sizeof(mpz_t) * length);
	int i;
	for (i=0; i<length; i++) {
		mpz_init_set_si(sequence->values[i], value);
	}
	return sequence;
}

void sequence_fill(sequence_t sequence, mpz_t value) {
	int i;
	for (i=0; i<sequence->length; i++) {
		mpz_set(sequence->values[i], value);
	}
}

void sequence_clear(sequence_t sequence) {
	int i;
	for (i=0; i<sequence->length; i++) {
		mpz_clear(sequence->values[i]);
	}
	free(sequence);
}

void sequence_print(sequence_t sequence) {
	int i;
	for (i=0; i<sequence->length; i++) {
		if (i==sequence->period) {
			printf(":");
		}
		if (i<sequence->length-1) {
			gmp_printf("%Zd,", sequence->values[i]);
		} else {
			gmp_printf("%Zd\n", sequence->values[i]);
		}
	}
}

void sequence_copy(sequence_t target, sequence_t source) {
	int i, length = MIN(source->length,target->length);
	for (i=0; i<length; i++) {
		mpz_set(target->values[i], source->values[i]);
	}
}

bool_t sequence_equals(sequence_t seq1, sequence_t seq2) {
	if (seq1==seq2) {
		return TRUE;
	}
	if ((seq1->length!=seq2->length) || (seq1->period!=seq2->period)) {
		return FALSE;
	}
	int i;
	for (i=0; i<seq1->length; i++) {
		if (mpz_cmp(seq1->values[i], seq2->values[i])!=0) {
			return FALSE;
		}
	}
	return TRUE;
}

hashcode_t sequence_hashcode(sequence_t sequence) {
	hashcode_t hashcode = 1;
	int i;
	for (i=0; i<sequence->length; i++) {
		hashcode = (hashcode * 31) + mpz_get_si(sequence->values[i]);
	}
	return (hashcode * 47) + sequence->period;
}
